﻿Public Class SearchGenericForm
    Dim source As String
    Public idSelected As Integer

    Private Sub SearchGenericForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GenericAdministrator.loadGrid(Me.dgvSearchTaskType, Me.bdnSearchTaskType, GenericAdministrator.searchDataByFilter(getSourceObject()))
        setDataGridViewHeaders()
    End Sub

    Private Sub txtId_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtId.KeyPress
        ViewValidators.isInteger(e)
    End Sub

    Private Sub txtId_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtId.TextChanged

    End Sub

    Private Sub cmdBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuscar.Click
        Dim id As Integer
        If Me.txtId.Text = Nothing Then
            id = 0
        Else
            id = Convert.ToInt16(Me.txtId.Text)
        End If
        Dim objectSource As GenericObject = getSourceObject(id, Me.txtNombre.Text, Me.txtDescripcion.Text)
        GenericAdministrator.loadGrid(Me.dgvSearchTaskType, Me.bdnSearchTaskType, GenericAdministrator.searchDataByFilter(objectSource))
    End Sub

    Private Sub setDataGridViewHeaders()
        Me.dgvSearchTaskType.Columns(0).HeaderText = "Codigo"
        Me.dgvSearchTaskType.Columns(1).HeaderText = "Nombre"
        Me.dgvSearchTaskType.Columns(2).HeaderText = "Descripcion"
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Dim opcion As Integer
        opcion = MsgBox("¿Está seguro que desea Eliminar el registro seleccionado?", vbYesNo + vbQuestion + vbDefaultButton2, "Salir")
        If opcion = vbYes Then
            GenericAdministrator.deletedById(getSourceObject(Me.dgvSearchTaskType.CurrentRow.Cells(0).Value, Nothing, Nothing))
            GenericAdministrator.loadGrid(Me.dgvSearchTaskType, Me.bdnSearchTaskType, GenericAdministrator.searchDataByFilter(getSourceObject()))
        End If
    End Sub

    Public Sub New(ByVal source As String)
        Me.InitializeComponent()

        Me.source = source
        customForm()
    End Sub

    Private Function getSourceObject() As GenericObject
        Select Case source
            Case "ROLE"
                Return New Role()
            Case "ACTIVITY"
                Return New Activity()
            Case "TASKTYPE"
                Return New TaskType()
        End Select
    End Function

    Private Function getSourceObject(ByVal id As Integer, ByVal name As String, ByVal description As String) As GenericObject
        Select Case source
            Case "ROLE"
                Return New Role(id, name, description)
            Case "ACTIVITY"
                Return New Activity(id, name, description)
            Case "TASKTYPE"
                Return New TaskType(id, name, description)
        End Select
    End Function

    Private Sub customForm()
        Select Case source
            Case "ROLE"
                Me.Text = "Consulta de Roles"
            Case "ACTIVITY"
                Me.Text = "Consulta de Actividades"
            Case "TASKTYPE"
                Me.Text = "Consulta de Tipos de Tareas"
        End Select
    End Sub

    Private Sub editar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editar.Click
        Dim idSelected As Integer
        idSelected = Me.dgvSearchTaskType.CurrentRow.Cells(0).Value
        Me.Close()
        Dim genericForm As New GenericForm(source, idSelected)
        genericForm.MdiParent = Me.MdiParent
        genericForm.Show()
    End Sub
End Class